using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using Entites;

namespace DAL
{
  public  class DonHangDAL
    {
            private SqlConnection cn = null;
        public DonHangDAL()
        {
            cn = new SqlConnection(ConfigurationManager.ConnectionStrings["strConnection"].ConnectionString);
            cn.Open();
        }
       public void Insert (DonHang dh )
        {
            SqlCommand cm = new SqlCommand("DonHang_insert", cn);
            cm.CommandType = CommandType.StoredProcedure;
            cm.Parameters.AddWithValue("@MaKH",dh.makh);
           cm.Parameters.AddWithValue("@NgayDatHang",dh.ngaydathang);
           cm.Parameters.AddWithValue("@TriGia",dh.trigia);
           cm.Parameters.AddWithValue("@NgayGiaoHang",dh.ngaygiaohang);
           cm.Parameters.AddWithValue("@TinhTrangGiaoHang",dh.tinhtranggiaohang);
           cm.Parameters.AddWithValue("@PhuongThucThanhToan",dh.phuongthucthanhtoan);
           cm.Parameters.AddWithValue("@TinhTrangThanhToan",dh.tinhtrangthanhtoan);
           cm.Parameters.AddWithValue("@NhanVienGiaoHang",dh.nhanviengiaohang);

            cm.ExecuteNonQuery();
        }
        public void Update(DonHang dh)
        {
            SqlCommand cm = new SqlCommand("DonHang_update", cn);
            cm.CommandType = CommandType.StoredProcedure;
            cm.Parameters.AddWithValue("@MaDH", dh.madh);
            cm.Parameters.AddWithValue("@MaKH",dh.makh);
           cm.Parameters.AddWithValue("@NgayDatHang",dh.ngaydathang);
           cm.Parameters.AddWithValue("@TriGia",dh.trigia);
           cm.Parameters.AddWithValue("@NgayGiaoHang",dh.ngaygiaohang);
           cm.Parameters.AddWithValue("@TinhTrangGiaoHang",dh.tinhtranggiaohang);
           cm.Parameters.AddWithValue("@PhuongThucThanhToan",dh.phuongthucthanhtoan);
           cm.Parameters.AddWithValue("@TinhTrangThanhToan",dh.tinhtrangthanhtoan);
           cm.Parameters.AddWithValue("@NhanVienGiaoHang",dh.nhanviengiaohang);
          
          
          
            cm.ExecuteNonQuery();
        }
        public void Delete(int iD)
        {
            SqlCommand cm = new SqlCommand("DonHang_delete", cn);
            cm.CommandType = CommandType.StoredProcedure;
            cm.Parameters.AddWithValue("@MaDH", iD);
            cm.ExecuteNonQuery();
        }

        public tbDonHangCollection Get(string where, int pageIndex, int pageSize,
            string orderBy, string orderDirection, out int totalRecords)
        {
            SqlCommand cm = new SqlCommand("DonHang_get", cn);
            cm.CommandType = CommandType.StoredProcedure;
            cm.Parameters.AddWithValue("@Where", where);
            cm.Parameters.AddWithValue("@PageIndex", pageIndex);
            cm.Parameters.AddWithValue("@PageSize", pageSize);
            cm.Parameters.AddWithValue("@OrderBy", orderBy);
            cm.Parameters.AddWithValue("@OrderDirection", orderDirection);
            cm.Parameters.Add(new SqlParameter("@TotalRecords", DbType.Int32)).Direction = ParameterDirection.Output;

            SqlDataReader dr = cm.ExecuteReader();
            tbDonHangCollection dhCollection = new tbDonHangCollection();

            while (dr.Read())
            {
                DonHang donhang = new DonHang();
               donhang.madh = int.Parse(dr.GetValue(0).ToString());
                donhang.makh = int.Parse(dr.GetValue(1).ToString());
                donhang.ngaydathang = DateTime.Parse(dr.GetValue(2).ToString());
                donhang.trigia = int.Parse(dr.GetValue(3).ToString());
                donhang.ngaygiaohang = DateTime.Parse(dr.GetValue(4).ToString());
                donhang.tinhtranggiaohang = int.Parse(dr.GetValue(5).ToString());
                donhang.phuongthucthanhtoan = int.Parse(dr.GetValue(6).ToString());
                donhang.tinhtrangthanhtoan = int.Parse(dr.GetValue(7).ToString());
                donhang.nhanviengiaohang = int.Parse(dr.GetValue(8).ToString());

                dhCollection.Add(donhang);
             }
            dr.Close();
            totalRecords = int.Parse(cm.Parameters["@TotalRecords"].Value.ToString());
            return dhCollection;
        }
    }
    
}
